package com.ttk.issuetracker.service;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ttk.issuetracker.dao.CompanyDao;
import com.ttk.issuetracker.dao.ProjectDao;
import com.ttk.issuetracker.model.Company;
import com.ttk.issuetracker.model.Project;
import com.ttk.issuetracker.model.User;
import com.ttk.issuetracker.vo.Result;
import com.ttk.issuetracker.vo.ResultFactory;

@Transactional
@Service("projectService")
public class ProjectServiceImpl extends AbstractService implements ProjectService {
	@Autowired
	ProjectDao projectDao;
	@Autowired
	CompanyDao companyDao;
	
	public ProjectServiceImpl() {
		super();
	}

	@Override
	public Result<Project> store(Integer idProject, Integer idCompany,
			String projectName, String actionUsername) {
		User actionUser = userDao.findByUsername(actionUsername);
        if (!actionUser.isAdmin()) {
            return ResultFactory.getFailResult(USER_NOT_ADMIN);
        }
		
		Project p;
		if(idProject == null) {
			p = new Project();
		} else {
			p = projectDao.find(idProject);
			if(p == null) {
				return ResultFactory.getFailResult("Unable find project with idProject [" + idProject + "]");
			}
		}
		
		p.setProjectName(projectName);
		
		Company company = null;
		if(idCompany != null) {
			company = companyDao.find(idCompany);
			if(company == null) {
				return ResultFactory.getFailResult("Unable find company with idCompany [" + idCompany + "]");
			} else {
				if(p.getCompany() != null && !p.getCompany().equals(company)) {
					p.getCompany().getProjects().remove(p);
					company.getProjects().add(p);
				} 
				p.setCompany(company);
			}
		}
		
		if(p.getId() == null) {
			projectDao.persist(p);
		} else {
			projectDao.merge(p);
		}
		
		return ResultFactory.getSuccessResult(p);
	}

	@Override
	public Result<Project> remove(Integer idProject, String actionUsername) {
		User actionUser = userDao.findByUsername(actionUsername);
        if (!actionUser.isAdmin()) {
            return ResultFactory.getFailResult(USER_NOT_ADMIN);
        }
		
		if(idProject == null) {
			return ResultFactory.getFailResult("Unable find project with idProject [" + idProject + "]");
		}
		
		Project p = projectDao.find(idProject);
		if(p == null) {
			return ResultFactory.getFailResult("Unable find project with idProject [" + idProject + "]");
		} else {
			if(p.getTasks().size() > 0) {
				return ResultFactory.getFailResult("Unable remove project with idProject [" + idProject + "] because of task assigned.");
			} else {
				projectDao.remove(p);
				return ResultFactory.getSuccessResultMsg("Remove project with idProject [" + idProject + "] successfully");
			}
		}
	}

	@Override
	public Result<Project> find(Integer idProject, String actionUsername) {
		if(!isValidUser(actionUsername)) {
			return ResultFactory.getFailResult(USER_INVALID);
		}
		
		if(idProject == null) {
			return ResultFactory.getFailResult("Unable find project with idProject [" + idProject + "]");
		}
		
		Project p = projectDao.find(idProject);
		if(p == null) {
			return ResultFactory.getFailResult("Unable find project with idProject [" + idProject + "]");
		} else {
			return ResultFactory.getSuccessResult(p);
		}
	}

	@Override
	public Result<List<Project>> findAll(String actionUsername) {
		if(!isValidUser(actionUsername)) {
			return ResultFactory.getFailResult(USER_INVALID);
		}
		
		List<Project> projects = projectDao.findAll();
		if(projects == null) {
			return ResultFactory.getFailResult("Unable fetch all project");
		} else {
			return ResultFactory.getSuccessResult(projects);
		}
	}
}
